import com.surelution.projects.congress.Requestmap;
import com.surelution.projects.congress.Role;
import com.surelution.projects.congress.User;
import com.surelution.projects.congress.UserRole;

class BootStrap {

	public static final String ROLE_ADMIN = "ROLE_ADMIN"
	public static final String ROLE_USER = "ROLE_USER"
	
	public static final String USER_ADMIN = "admin"
	
	private static final def REQUESTMAP = [
		"/requestmap/**":"hasRole('ROLE_ADMIN')",
		"/user/**":"hasRole('ROLE_ADMIN')",
		"/party/**":"hasRole('ROLE_ADMIN')",
		"/checkInTimer/**":"hasRole('ROLE_ADMIN')",
		"/":"IS_AUTHENTICATED_FULLY",
		"/index.gsp":"IS_AUTHENTICATED_FULLY"
	]

    def init = { servletContext ->
		def roleAdmin = Role.findByAuthority(ROLE_ADMIN)
		if(!roleAdmin) {
			roleAdmin = new Role(authority:ROLE_ADMIN)
			roleAdmin.save(flush:true)
		}
		def roleUser = Role.findByAuthority(ROLE_USER)
		if(!roleUser) {
			roleUser = new Role(authority:ROLE_USER)
			roleUser.save(flush:true)
		}
		def userAdmin = User.findByUsername(USER_ADMIN)
		if(!userAdmin) {
			userAdmin = new User(username:USER_ADMIN, password:"admin", enabled:true)
			userAdmin.save(flush:true)
			UserRole.create(userAdmin, roleAdmin, true)
		}
		
		REQUESTMAP.each {
			def requestmap = Requestmap.findByUrl(it.key)
			if(!requestmap) {
				requestmap = new Requestmap(url:it.key, configAttribute:it.value)
				requestmap.save(flush:true)
			}
		}
		
    }
    def destroy = {
    }
}
